package com.ruoyi.crm.bid.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.crm.bid.domain.CrmBidModelColumn;
import com.ruoyi.crm.bid.mapper.CrmBidModelMapper;
import com.ruoyi.crm.bid.domain.CrmBidModel;
import com.ruoyi.crm.bid.service.ICrmBidModelService;

/**
 * 模型管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-10-26
 */
@Service
public class CrmBidModelServiceImpl implements ICrmBidModelService 
{
    @Autowired
    private CrmBidModelMapper crmBidModelMapper;

    /**
     * 查询模型管理
     * 
     * @param id 模型管理ID
     * @return 模型管理
     */
    @Override
    public CrmBidModel selectCrmBidModelById(Long id)
    {
        return crmBidModelMapper.selectCrmBidModelById(id);
    }

    /**
     * 查询模型管理列表
     * 
     * @param crmBidModel 模型管理
     * @return 模型管理
     */
    @Override
    public List<CrmBidModel> selectCrmBidModelList(CrmBidModel crmBidModel)
    {
        return crmBidModelMapper.selectCrmBidModelList(crmBidModel);
    }

    /**
     * 新增模型管理
     * 
     * @param crmBidModel 模型管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCrmBidModel(CrmBidModel crmBidModel)
    {
    	crmBidModel.setCreateTime(DateUtils.getNowDate());
    	crmBidModel.setUpdateTime(DateUtils.getNowDate());
    	crmBidModel.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
        int rows = crmBidModelMapper.insertCrmBidModel(crmBidModel);
        insertCrmBidModelColumn(crmBidModel);
        return rows;
    }

    /**
     * 修改模型管理
     * 
     * @param crmBidModel 模型管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCrmBidModel(CrmBidModel crmBidModel)
    {
    	crmBidModel.setUpdateTime(DateUtils.getNowDate());
    	crmBidModel.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
        crmBidModelMapper.deleteCrmBidModelColumnByModelId(crmBidModel.getId());
        insertCrmBidModelColumn(crmBidModel);
        return crmBidModelMapper.updateCrmBidModel(crmBidModel);
    }

    /**
     * 批量删除模型管理
     * 
     * @param ids 需要删除的模型管理ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCrmBidModelByIds(Long[] ids)
    {
        crmBidModelMapper.deleteCrmBidModelColumnByModelIds(ids);
        return crmBidModelMapper.deleteCrmBidModelByIds(ids);
    }

    /**
     * 删除模型管理信息
     * 
     * @param id 模型管理ID
     * @return 结果
     */
    @Override
    public int deleteCrmBidModelById(Long id)
    {
        crmBidModelMapper.deleteCrmBidModelColumnByModelId(id);
        return crmBidModelMapper.deleteCrmBidModelById(id);
    }

    /**
     * 新增模型字段信息
     * 
     * @param crmBidModel 模型管理对象
     */
    public void insertCrmBidModelColumn(CrmBidModel crmBidModel)
    {
        List<CrmBidModelColumn> crmBidModelColumnList = crmBidModel.getCrmBidModelColumnList();
        Long id = crmBidModel.getId();
        if (StringUtils.isNotNull(crmBidModelColumnList))
        {
            List<CrmBidModelColumn> list = new ArrayList<CrmBidModelColumn>();
            for (CrmBidModelColumn crmBidModelColumn : crmBidModelColumnList)
            {
                crmBidModelColumn.setModelId(id);
                crmBidModelColumn.setCreateTime(DateUtils.getNowDate());
                crmBidModelColumn.setUpdateTime(DateUtils.getNowDate());
                crmBidModelColumn.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
                crmBidModelColumn.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
                list.add(crmBidModelColumn);
            }
            if (list.size() > 0)
            {
                crmBidModelMapper.batchCrmBidModelColumn(list);
            }
        }
    }
}
